package com.success.application.myapplication.nav;

import android.content.Context;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.success.application.base.BaseRecyclerViewFragment;
import com.success.application.base.BaseViewPagerFragment;
import com.success.application.ground.FriendFragment;
import com.success.application.ground.IndexFragment;
import com.success.application.ground.bean.Friend;
import com.success.application.myapplication.R;
import com.success.application.test.TestViewPagerFragment;
import com.success.application.user.UserInfoFragment;
import com.success.application.widget.BorderShape;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NavFragment extends Fragment {

    private Context mContext;
    protected View mRoot;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationButton mCurrentNavButton;

    @Bind(R.id.nav_item_index)
    NavigationButton mNavIndex;
    @Bind(R.id.nav_item_chat)
    NavigationButton mNavChat;
    @Bind(R.id.nav_item_ground)
    NavigationButton mNavGround;
    @Bind(R.id.nav_item_me)
    NavigationButton mNavMe;

    public NavigationButton getmNavIndex() {
        return mNavIndex;
    }

    public void setmNavIndex(NavigationButton mNavIndex) {
        this.mNavIndex = mNavIndex;
    }

    public NavigationButton getmNavChat() {
        return mNavChat;
    }

    public void setmNavChat(NavigationButton mNavChat) {
        this.mNavChat = mNavChat;
    }

    public NavigationButton getmNavGround() {
        return mNavGround;
    }

    public void setmNavGround(NavigationButton mNavGround) {
        this.mNavGround = mNavGround;
    }

    public NavigationButton getmNavMe() {
        return mNavMe;
    }

    public void setmNavMe(NavigationButton mNavMe) {
        this.mNavMe = mNavMe;
    }

    public NavFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NavFragment.
     */
    public static NavFragment newInstance(String param1, String param2) {
        NavFragment fragment = new NavFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager = getActivity().getSupportFragmentManager();
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_nav, container, false);
        ButterKnife.bind(this,root);

        ShapeDrawable lineDrawable = new ShapeDrawable(new BorderShape(new RectF(0, 1, 0, 0)));
        lineDrawable.getPaint().setColor(getResources().getColor(R.color.list_divider_color));
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{
                new ColorDrawable(getResources().getColor(R.color.white)),
                lineDrawable
        });
        root.setBackgroundDrawable(layerDrawable);

        mNavIndex.init(R.drawable.tab_icon_index,
                R.string.index_title,
                TestViewPagerFragment.class);

        mNavChat.init(R.drawable.tab_icon_chat,
                R.string.index_chat,
                TestViewPagerFragment.class);

        mNavGround.init(R.drawable.tab_icon_ground,
                R.string.index_ground,
                IndexFragment.class);

        mNavMe.init(R.drawable.tab_icon_me,
                R.string.index_me,
                UserInfoFragment.class);

        return root;
    }

    @OnClick({R.id.nav_item_index, R.id.nav_item_chat,
            R.id.nav_item_ground, R.id.nav_item_me})
    public void onClick(View v) {
        if (v instanceof NavigationButton) {
            NavigationButton nav = (NavigationButton) v;
            doSelect(nav);
        }
    }
    private void doSelect(NavigationButton newNavButton) {
        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), null);
                ft.add(R.id.main_container, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }

    private void onReselect(NavigationButton navigationButton) {
        Fragment fragment = navigationButton.getFragment();
        if(fragment instanceof BaseRecyclerViewFragment){
            ((BaseRecyclerViewFragment)fragment).onTabReselect();
        }
        if(fragment instanceof BaseViewPagerFragment){
            ((BaseViewPagerFragment)fragment).onTabReselect();
        }
//        if(fragment instanceof IndexFragment){
//            ((IndexFragment)fragment).onTabReselect();
//        }
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        // do clear
        clearOldFragment();
        doSelect(mNavIndex);
    }

    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commitNow();
    }
}
